"""
Case Type   : Check_Quorum_Consistency
Case Name   : gs_guc set方式修改enable_save_confirmed_lsn参数值
Create At   : 2022/10/24
Owner       : liu-tong-8848
Description :
    1.查询默认值
    2.gs_guc set方式修改enable_save_confirmed_lsn参数值为on
    3.gs_guc set方式修改enable_save_confirmed_lsn参数值为on,off之外的值
    4.gs_guc set方式修改enable_save_confirmed_lsn参数值为off
Expect      :
    1.默认值为off
    2.修改成功
    3.修改失败，合理报错
    4.修改成功
History     :
"""
import os
import unittest
from testcase.utils.Logger import Logger
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant


class CheckQuorumConsistency(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(f"-----{os.path.basename(__file__)} start-----")
        self.primary_sh = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.common = Common()
        self.config = 'enable_save_confirmed_lsn'

    def test_check_quorum_consistency(self):
        text = "-----step1:查询默认值;expect:默认值为off-----"
        self.logger.info(text)
        show_msg = self.common.show_param(self.config)
        self.assertEquals(show_msg, 'off', '执行失败' + text)

        text = "-----step2:gs_guc set方式修改enable_save_confirmed_lsn" \
               "参数值为on;expect:修改成功-----"
        self.logger.info(text)
        result = self.primary_sh.execute_gsguc('set',
                                               self.constant.GSGUC_SUCCESS_MSG,
                                               f'{self.config}=on')
        self.assertTrue(result, '执行失败' + text)
        restart_res = self.primary_sh.restart_db_cluster()
        self.assertTrue(restart_res, '执行失败' + text)
        show_msg = self.common.show_param(self.config)
        self.assertEquals(show_msg, 'on', '执行失败' + text)

        text = "-----step3:gs_guc set方式修改enable_save_confirmed_lsn" \
               "参数值为on,off之外的值;expect:修改失败，合理报错-----"
        self.logger.info(text)
        result = self.primary_sh.execute_gsguc('set',
                                               self.constant.GSGUC_SUCCESS_MSG,
                                               f'{self.config}=ab')
        self.assertFalse(result, '执行失败' + text)
        result = self.primary_sh.execute_gsguc('set',
                                               self.constant.GSGUC_SUCCESS_MSG,
                                               f'{self.config}=123')
        self.assertFalse(result, '执行失败' + text)
        result = self.primary_sh.execute_gsguc('set',
                                               self.constant.GSGUC_SUCCESS_MSG,
                                               f'{self.config}=@on*')
        self.assertFalse(result, '执行失败' + text)
        result = self.primary_sh.execute_gsguc('set',
                                               self.constant.GSGUC_SUCCESS_MSG,
                                               f'{self.config}=#23$')
        self.assertFalse(result, '执行失败' + text)
        show_msg = self.common.show_param(self.config)
        self.assertEquals(show_msg, 'on', '执行失败' + text)

    def tearDown(self):
        text = "-----step4:gs_guc set方式修改enable_save_confirmed_lsn" \
               "参数值为off;expect:修改成功-----"
        self.logger.info(text)
        guc_res = self.primary_sh.execute_gsguc('set',
                                                self.constant.GSGUC_SUCCESS_MSG,
                                                f'{self.config}=off')
        restart_res = self.primary_sh.restart_db_cluster()
        show_msg = self.common.show_param(self.config)
        self.assertTrue(guc_res, '执行失败' + text)
        self.assertTrue(restart_res, '执行失败' + text)
        self.assertEquals(show_msg, 'off', '执行失败' + text)
        self.logger.info(f"-----{os.path.basename(__file__)} end-----")
